package com.str.stack;
import java.io.*;

public class StackTest {
	
  private static Stack createStack(int i) {
  	if (i == 1) return new ArrayStack();
    if (i == 2) return new LinkedStack();
  	return null;
  }   

  public static void main(String args[]) throws IOException {
    BufferedReader stdin = new BufferedReader(
                           new InputStreamReader(System.in));
    
    while (true) {
      System.out.println("1. Create Array Stack");
      System.out.println("2. Create Linked Stack");    	         
      System.out.print("\tEnter your choice(Enter to quit): ");          
      
      String inputLine = stdin.readLine();
      if (inputLine.equals("")) {
        System.out.println("End of run.");
        System.exit(0);
      }     
      
      int choice = Integer.parseInt(inputLine);
      Stack strStack = createStack(choice);
      if (strStack == null) {   	
        System.out.println("Invalid choice.");
        continue;
      }   
    
      while (true) {
        System.out.println("-------------------------");
        System.out.println("     1. Clear");
        System.out.println("     2. Push");    
        System.out.println("     3. Pop");
        System.out.println("     4. Peek");
        System.out.print("\tEnter your choice(Enter to quit): ");       
        
        inputLine = stdin.readLine();      
        if (inputLine.equals("")) break;  
        choice = Integer.parseInt(inputLine);

        switch (choice) {      
          case 1:
            System.out.println("Cleared.");
            strStack.clear();
            break;

          case 2:
            System.out.print("Enter a string: ");
            Object val = stdin.readLine();
            strStack.push(val);
            break;

          case 3:
            if (strStack.isEmpty())
              System.out.println("Stack is empty.");
            else {
              val = strStack.pop();
              System.out.println("The popped value is " + val);
            }
            break;

          case 4:
            if (strStack.isEmpty())
              System.out.println("Stack is empty.");
            else {
              val = strStack.peek();
              System.out.println("The top value is " + val);
            }
            break;

          default:
            System.out.println("Invalid choice.");
            continue;
        }
      
        System.out.println(strStack);
        System.out.println("Size = " + strStack.size());
      }
    }
  }
}
